<?php

namespace App\Http\Controllers\ChinaBank\Admin;

use App\Models\ChinaBank\CardModel;
use App\Models\ChinaBank\CardTypeModel;
use App\Models\ChinaBank\GoodsModel;
use App\Models\ChinaBank\ShopModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\ChinaBank\Admin\CardImport;

class CardController extends Controller
{
    public function cards(Request $request){

        $account    = $request->input('account','');
        $pwd        = $request->input('pwd','');
        $order      = $request->input('order',-1);
        $type       = $request->input('type',0);
        $params     = [
            "account"   => $account,
            "pwd"       => $pwd,
            "order"     => $order,
            "type"      => $type
        ];

        $cards  = CardModel::whereNull("deleted_at");
        if($account){
            $cards->where('account','like',"%".$account."%");
        }

        if($pwd){
            $cards->where('password','like',"%".$pwd."%");
        }

        if($order > 0){
            $cards->where(function($db){
                $db->where('order_id','>',0)->orWhere('bank_order_id','>',0);
            });
        }
        if($type){
            $cards->where('type',$type);
        }
        $cards = $cards->with('cardtype')->orderBy('card_id','desc')->paginate(15);
        $types  = CardTypeModel::whereNull('deleted_at')->get();
        return self::adminview("card/card-list",['cards'=>$cards,'types'=>$types,"params"=>$params]);
    }

    public function cardDel(Request $request){
        $id     = $request->input('id');
        $ids    = explode(",",$id);
        foreach($ids as $id){
            CardModel::where("card_id",$id)->where('order_id',0)->delete();
        }
        return self::send();
    }


    public function importCard(Request $request)
    {
        //读取文件
        $cards = Excel::toArray(new CardImport,$request->file('file'));
        $cards = $cards[0];
        $title = array_splice($cards,0,1);
        $title = $title[0];
        ## 卡号 密码 批次 类型 有效日期
        //检查文件有效性
        $accounts   = [];
        $validCards = [];
        foreach($cards as $key => $card)
        {
            if(count(array_filter($cards)) == 0){
                continue;
            }

            $valid = $card[0]&&$card[1]&&$card[2]&&$card[3]&&$card[4];
            if(!$valid)
            {
                return self::send(2001,($key + 2)."行有无效数据");
            }

            $accounts[] = $card[0];
            $validCards[] = [
                "account"   => $card[0],
                "password"  => $card[1],
                "batch_code"=> $card[2],
                "type"      => $card[3],
                "valid_date"=> $card[4]
            ];
        }

        $card = CardModel::whereIn('account',$accounts)->first();
        if($card){
            return self::send(2002,"重复卡号:".$card->account);
        }

        foreach($validCards as $card)
        {
            (new CardModel($card))->save();
        }

        return self::send();
    }


    public function types(){
        $types = CardTypeModel::whereNull('deleted_at')->get();
        foreach($types as $type){
            $goods = explode(",",$type->goods);
            $type->goods = GoodsModel::whereIn('id',$goods)->get();
        }
        return self::adminview("card/card-type-list",['types'=>$types]);
    }

    public function typeEdit(Request $request){

        $id     = $request->input('id',0);
        if($request->method() == 'POST'){

            $data   = [
                "name"  => $request->input('name'),
                "goods" => $request->input('goods'),
            ];
            if($id == 0){
                CardTypeModel::create($data);
            }else{
                CardTypeModel::where('id',$id)->update($data);
            }
            return self::send();
        }

        if($id > 0){
            $type = CardTypeModel::find($id);
        }else{
            $type = new \stdClass();
            $type->name = "";
            $type->id = 0;
            $type->goods = 0;
        }

        $goods = GoodsModel::with('shopinfo')->get();


        return self::adminview("card/type-edit",['type'=>$type,'goods'=>$goods]);
    }

    public function typeDel(Request $request){
        $id     = $request->input('id');
        CardTypeModel::where('id',$id)->delete();
        return self::send();
    }
}
